"use strict"

var Router = require('koa-router');
var config = require('../../../config/config.js');
var dao = require('../../../dao/mongo/shop.js');
var debug = require('debug')('shop');

console.log('router:' + config.baseUrl + '/v1/shop');

var router = new Router({
    prefix: config.baseUrl + '/v1/shop'
});

// router.get('/test', function*() {
//   this.body = 'test work fine';
// });

/**
 * 添加商铺接口
 {
   "name": "shop-test",
   "address": "天河区天河路208号粤海喜来登酒店8楼",
   "telephone": "020-66822580",
   "description": "一家店",
   "images_url": ["https://www.baidu.com/img/bdlogo.png"],
   "web_url":"https://www.baidu.com/",
   "distinct_code": 123,
   "loc": [23.13268,113.322],
   "cate_codes": [1,2],
 }
 */
router.post('/', function*() {
    var vendorId = this.session.vendorId;
    if (!vendorId) {
        this.status = 402;
        this.body = 'require login';
        return;
    }
    this.request.inParams.vendorId = vendorId;
    var result = yield dao.save(this.request.inParams);
    this.body = result;
});

/**获取我的shop*/
router.get('/myshop', function*() {
    debug('/myshop');

    var vendorId = this.session.vendorId;
    if (!vendorId) {
        this.status = 402;
        this.body = 'require login';
        return;
    }

    var query = {
        vendor_id: vendorId
    };
    var result = yield dao.findByVendorId(vendorId);
    this.body = result;
});

module.exports = router.routes();